/*
 * 铁路道岔故障检测信息管理系统
 * Design by 魏梓晟
 * Version :1.0
 */
package com.railway.switchs.service.impl.authority;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.railway.switchs.dao.authority.AuthorityRolePermissionDao;
import com.railway.switchs.domain.dto.authority.AuthorityRolePermission;
import com.railway.switchs.domain.query.authority.AuthorityRolePermissionQuery;
import com.railway.switchs.service.authority.IAuthorityRolePermissionService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * authority_role_permission - 角色资源关联表 Service实现类
 * @author 魏梓晟
 */
@Service("authorityRolePermissionService")
public class AuthorityRolePermissionServiceImpl implements IAuthorityRolePermissionService {

    
    @Autowired
    private AuthorityRolePermissionDao authorityRolePermissionDao;

	private final int QUERY_LIMLIT = 10000;

    @Override
	@Transactional(propagation= Propagation.REQUIRED,timeout=30,rollbackFor={RuntimeException.class, Exception.class})
    public Long add(AuthorityRolePermission record) {
		record.setCreateTime(new Date());
		record.setVersionNum(1L);
        if(authorityRolePermissionDao.add(record)==1){
			return record.getId();
		}
		return 0L;
    }

    @Override
	@Transactional(propagation= Propagation.REQUIRED,timeout=30,rollbackFor={RuntimeException.class, Exception.class})
    public boolean edit(AuthorityRolePermission record) {
		record.setModifyTime(new Date());
		record.setVersionNum(authorityRolePermissionDao.get(record.getId()).getVersionNum());
        return authorityRolePermissionDao.update(record)==1;
    }

    @Override
	@Transactional(propagation= Propagation.REQUIRED,timeout=30,rollbackFor={RuntimeException.class, Exception.class})
    public boolean delete(Long id) {

        return authorityRolePermissionDao.delete(id)==1;
    }

    @Override
	@Transactional(propagation= Propagation.REQUIRED,timeout=30,rollbackFor={RuntimeException.class, Exception.class})
    public AuthorityRolePermission get(Long id) {

        return authorityRolePermissionDao.get(id);
    }

    @Override
	@Transactional(propagation= Propagation.REQUIRED,timeout=30,rollbackFor={RuntimeException.class, Exception.class})
    public AuthorityRolePermissionQuery query(AuthorityRolePermissionQuery query) {
        int count =  authorityRolePermissionDao.count(query);
        query.setCount(count);
        query.setData(authorityRolePermissionDao.query(query));
        return query;
    }

    @Override
	@Transactional(propagation= Propagation.REQUIRED,timeout=30,rollbackFor={RuntimeException.class, Exception.class})
    public List<AuthorityRolePermission> getList(AuthorityRolePermissionQuery query) {
        int count = authorityRolePermissionDao.count(query);
        int pags = (count - 1) / QUERY_LIMLIT + 1;
        List<AuthorityRolePermission> authorityRolePermissionList = new ArrayList<>(count);
        for(int i = 1 ;i <= pags ; i++ ){
            query.setPage(i);
            query.setLimit(QUERY_LIMLIT);
            authorityRolePermissionList.addAll(authorityRolePermissionDao.query(query));
        }
        return authorityRolePermissionList;
    }
}
